/*
*    Phantom v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "StdAfx.h"
#include ".\windowfactory.h"

CWindowFactory* CWindowFactory::theFactory = 0;

int Phantom_RegisterWindow(CPhantomWindow* RepWindow){
	return CWindowFactory::getInstance()->RegisterWindow(RepWindow);
}

int Phantom_ClearWindows(){
	return CWindowFactory::getInstance()->ClearWindows();
}

CPhantomWindow* Phantom_WindowFromClass(String& Class){
	return CWindowFactory::getInstance()->WindowFromClass(Class);
}

CWindowFactory::CWindowFactory(void)
: TypeCtr(1)
{
	FirstReg = 0;
}

CWindowFactory::~CWindowFactory(void)
{
	DelTree(FirstReg);
}

int CWindowFactory::RegisterWindow(CPhantomWindow* RepWindow)
{
	if(!RepWindow) return -1;

	PhantomWindowNode Curr;
	if(FirstReg == SSI_NULL){
		FirstReg = NewNode();
		Curr = FirstReg;
	}
	else{
		Curr = FirstReg;
		if(Curr->aWin->GetClass() == RepWindow->GetClass()){
			return Curr->Type;
		}	

		// If this class name is already registered, return its type
		while(Curr->Next){
			Curr = Curr->Next;
			if(Curr->aWin->GetClass() == RepWindow->GetClass()){
				return Curr->Type;
			}	
		}
		//if(Curr->aWin->GetClass() == RepWindow->GetClass()){
		//	return Curr->Type;
		//}
		//while(Curr->Next){
		//	Curr = Curr->Next;
		//}
		Curr->Next = NewNode();
		Curr = Curr->Next;
	}

	// Initialize the Data Member Functions
	if(!RepWindow->InitWindow()){
		delete Curr->Next;
		Curr->Next = SSI_NULL;
		return -1;
	}

	Curr->aWin = RepWindow;
	Curr->Type = GetType();
	return Curr->Type;
}

int CWindowFactory::GetType(void)
{
	return TypeCtr++;
}

PhantomWindowNode CWindowFactory::NewNode(void)
{
	PhantomWindowNode aNode = new PhantomWindowT;
	aNode->aWin = SSI_NULL;
	aNode->Next = SSI_NULL;
	aNode->Type = 0;
	return aNode;
}

CPhantomWindow* CWindowFactory::WindowFromClass(String& Class)
{
	PhantomWindowNode aNode = FirstReg;
	CPhantomWindow* Ret;
	CPhantomWindow* Temp;
	while(aNode){
		if(aNode->aWin->GetClass() == Class){
			return aNode->aWin->MakeWindow();
		}else{
			aNode = aNode->Next;
		}
	}

	// Return a default basic window
	Temp = new CPhantomWindow();
	Ret = Temp->MakeWindow();
	Ret->SetClass(Class);
	delete Temp;
	return Ret;
}

void CWindowFactory::DelTree(PhantomWindowNode aNode)
{
	if(!aNode) return;
	if(aNode->Next) DelTree(aNode->Next);
	if(aNode->aWin){
		delete aNode->aWin;
	}
	delete aNode;
}

int CWindowFactory::ClearWindows(void)
{
	delete theFactory;
	theFactory = 0;
	return 1;
}
